/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package protect01;

/**
 *
 * @author reshetnikovk
 */
public class MainFrame extends javax.swing.JFrame {

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
        
        String al = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        alfa = al.toCharArray();
        
        refreshDict("DIPLOMAT", 5);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jsK = new javax.swing.JSpinner();
        jtfKeyWord = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtaDict = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtaDecoded = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        jtaEncoded = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jbEncode = new javax.swing.JButton();
        jbDecode = new javax.swing.JButton();
        jbRefresh = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jsK.setOpaque(false);
        jsK.setValue(5);

        jtfKeyWord.setText("DIPLOMAT");

        jtaDict.setColumns(20);
        jtaDict.setRows(5);
        jScrollPane1.setViewportView(jtaDict);

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

        jtaDecoded.setColumns(20);
        jtaDecoded.setRows(5);
        jScrollPane2.setViewportView(jtaDecoded);

        jPanel1.add(jScrollPane2);

        jtaEncoded.setColumns(20);
        jtaEncoded.setRows(5);
        jScrollPane3.setViewportView(jtaEncoded);

        jPanel1.add(jScrollPane3);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        jbEncode.setText("Шифровать");
        jbEncode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEncodeActionPerformed(evt);
            }
        });
        jPanel2.add(jbEncode, new java.awt.GridBagConstraints());

        jbDecode.setText("Дешифровать");
        jbDecode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbDecodeActionPerformed(evt);
            }
        });
        jPanel2.add(jbDecode, new java.awt.GridBagConstraints());

        jbRefresh.setText("Обновить");
        jbRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbRefreshActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jsK, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfKeyWord)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbRefresh))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jsK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfKeyWord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbRefresh))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbRefreshActionPerformed
        refreshDict();
    }//GEN-LAST:event_jbRefreshActionPerformed

    private void jbEncodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbEncodeActionPerformed
        jtaEncoded.setText(d.encodeString(jtaDecoded.getText()));
    }//GEN-LAST:event_jbEncodeActionPerformed

    private void jbDecodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbDecodeActionPerformed
        jtaDecoded.setText(d.decodeString(jtaEncoded.getText()));
    }//GEN-LAST:event_jbDecodeActionPerformed

    Dict d;
    char[] alfa;
    
    private void refreshDict() {
        System.out.println(String.format("%s %d", jtfKeyWord.getText(), (Integer) jsK.getValue()));
        refreshDict(jtfKeyWord.getText(), (Integer) jsK.getValue());
    }
    
    private void refreshDict(String keyWord, int k) {
        d = new Dict(alfa, keyWord, k);
        jtaDict.setText(d.toString());
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JButton jbDecode;
    private javax.swing.JButton jbEncode;
    private javax.swing.JButton jbRefresh;
    private javax.swing.JSpinner jsK;
    private javax.swing.JTextArea jtaDecoded;
    private javax.swing.JTextArea jtaDict;
    private javax.swing.JTextArea jtaEncoded;
    private javax.swing.JTextField jtfKeyWord;
    // End of variables declaration//GEN-END:variables
}
